package cn.steel.utils;

/**
 * @author: zhaoyuhan
 * @date: 2025/4/29
 * @description: 接口定义
 */

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class AuthContext {
    private static final String AUTH_USER_KEY = "authUser";

    public static AuthUser getCurrentUser() {
        HttpServletRequest request = (HttpServletRequest) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return (AuthUser) request.getSession().getAttribute(AUTH_USER_KEY);
    }

    public static void setCurrentUser(HttpServletRequest request, AuthUser user) {
        request.getSession().setAttribute(AUTH_USER_KEY, user);
    }

    public static void clearCurrentUser(HttpServletRequest request) {
        request.getSession().removeAttribute(AUTH_USER_KEY);
    }
}